# -*- coding: utf-8 -*-
# @Author   : xiongjianwen
# @Time     : 2022/10/31 8:59
# @File     : winappdriver_demo.py
# @Project  : BwProCost_Auto_v2

import time, os

from appium import webdriver
from pywinauto.keyboard import parse_keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep


class Auto():

    def open_software(self, host='localhost', port=4723):
        # 打开WinAppDriver服务
        # 注意：如果手动开启，则可以注释掉
        os.system(r'start "" /d "C:\Program Files\Windows Application Driver\"  "WinAppDriver.exe"')

        # 配置信息
        # 包含：平台名、系统、应用程序绝对路径
        desired_caps = {'platformName': 'Windows', 'deviceName': 'WindowsPC',
                        # 'app': 'Root'}
                        'app':r"D:\NewPlat\Zw\Bin\AppStarter.exe",'ms:waitForAppLaunch':5}

        try:
            # 连接WinAppDriver服务，打开目标软件
            self.driver = webdriver.Remote('http://{}:{}'.format(host, port), desired_caps)
            # print(self.driver.current_window_handle)
        except Exception as e:
            raise AssertionError(e)

    def login_software(self):
        # sleep(3)
        # self.driver.find_element_by_name('登录"网络锁"').click() # 改成插单机锁，网络锁读不到url
        self.driver.find_element_by_name('确定').click() # 这种快---与uiautomation差不多
        # sleep(2)
        # self.driver.find_element_by_name('登录').click()

    def test_software(self):
        # sleep(5)
        window_handle = self.driver.window_handles[0]
        print(window_handle)
        self.driver.switch_to.window(window_handle)
        # self.driver.find_element_by_xpath('//Button[starts-with(@Name,"打    开")]').click() # 这种慢，类似于uiautomation的定位
        self.driver.find_element_by_name('打   开').click()
        self.driver.find_element_by_name('文件名(N):').send_keys(r'D:/NewPlat/变电站工程.zwzj')
        self.driver.find_element_by_name('打开(O)').click()

class Auto2:
    def test(self, host='localhost', port=4723):
        desired_caps = {'appTopLevelWindow':'0x00100E2A'}
        try:
            self.driver = webdriver.Remote(f'http://{host}:{port}', desired_caps)
        except Exception as e:
            raise AssertionError(e)
        print(self.driver.window_handles)
        # self.driver.switch_to.window('0x00510ADC')
        # self.driver.find_element_by_name('打   开').click()
        # self.driver.find_element_by_name('文件名(N):').send_keys(r'D:/NewPlat/变电站工程.zwzj')
        # 输入文件路径
        # self.driver.find_element_by_name('打开(O)').click()
        # print(app_windows.DataItemControl(Name='供货方').GetNextSiblingControl().Name)
        self.driver.find_element_by_name('循环流化床锅炉').click() # 耗时与uiautomation差不多，也要很久的时间，故该方案可以放弃
        print(self.driver.find_element_by_name('供货方').text)
        # print(app_windows.TreeItemControl(Name='循环流化床锅炉').GetNextSiblingControl().Name)


if __name__ == '__main__':
    # auto = Auto()
    # auto.open_software()
    # auto.login_software()
    # auto.test_software()
    auto2 = Auto2()
    auto2.test()